<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * Treacle 缓存驱动抽象类文件
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id$
 * @package caching
 */

/**
 * 缓存驱动抽象类
 *
 * @package	caching
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 * @abstract Cache_driver
 */

abstract class Cache_driver
{
	/**
	 * key的前缀
	 *
	 * @var string
	 */
	protected $_prefix = null;
	/**
	 * 缓存目录
	 *
	 * @var string
	 */
	protected $_cachedir = '';

	/**
	 * 构造函数
	 *
	 */
	public function __construct()
	{
		$this->_cachedir = APPPATH.'tmp/_cache/';
	}

	/**
	 * 设置缓存key的前缀
	 * 
	 * @param string a unique prefix for the keys of cached values
	 */
	public function setKeyPrefix($value)
	{
		$this->_prefix = $value;
	}
	/**
	 * 重组key
	 * 将设置的key前缀与key组合
	 * 
	 * @access private
	 * @param string 没有组合的key
	 * @return sring 一个组合后的key
	 */
	private function _UniqueKey($key)
	{
		return md5($this->_prefix.$key);
	}
	/**
	 * 按key获得一个缓存的值
	 * 
	 * @param string 需要获取缓存的key
	 * @return mixed the value stored in cache, false if the value is not in the cache or expired.
	 */
	public function get($id)
	{
		if(($value=$this->getValue($this->_UniqueKey($id)))!==false)
		{
			return $value;
		}
		return null;
	}
	/**
	 * 设置一个缓存
	 * 
	 * 如果该缓存不存在,将会被创建
	 *
	 * @param string the key identifying the value to be cached
	 * @param mixed the value to be cached
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
	 * @param ICacheDependency dependency of the cached item. If the dependency changes, the item is labelled invalid.
	 * @return boolean true if the value is successfully stored into cache, false otherwise
	 */
	public function set($id,$value,$expire=100)
	{
		$expire	= ($expire<=0)?0:time()+$expire;
		return $this->setValue($this->_UniqueKey($id),$value,$expire);
	}
	/**
	 * 创建一个缓存
	 * 
	 * @param string the key identifying the value to be cached
	 * @param mixed the value to be cached
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
	 * @param ICacheDependency dependency of the cached item. If the dependency changes, the item is labelled invalid.
	 * @return boolean true if the value is successfully stored into cache, false otherwise
	 */
	public function add($id,$value,$expire=100,$dependency=null)
	{
		$data	= array($value,$dependency);
		$expire	= ($expire<=0)?0:time()+$expire;
		return $this->addValue($this->_UniqueKey($id),serialize($data),$expire);
	}
	/**
	 * 删除一个缓存的值,保留key
	 * 
	 * @param string the key of the value to be deleted
	 * @return boolean if no error happens during deletion
	 */
	public function delete($id)
	{
		return $this->deleteValue($this->_UniqueKey($id));
	}
	/**
	 * 清除所有缓存内容.
	 * 
	 * 抽象方法，子类使用
	 * 
	 * @return boolean 是否清除成功
	 */
	abstract public function flush();
	/**
	 * 按key来获取一个缓存的值.
	 * 
	 * 抽象方法，子类使用
	 * 
	 * @param string a unique key identifying the cached value
	 * @return string the value stored in cache, false if the value is not in the cache or expired.
	 */
	abstract protected function getValue($key);
	/**
	 * 设置缓存
	 * 
	 * 抽象方法，子类使用
	 * 
	 * @param string the key identifying the value to be cached
	 * @param string the value to be cached
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
	 * @return boolean true if the value is successfully stored into cache, false otherwise
	 */
	abstract protected function setValue($key,$value,$expire);
	/**
	 * 创建一个缓存key
	 * 
	 * 抽象方法，子类使用
	 * 
	 * @param string the key identifying the value to be cached
	 * @param string the value to be cached
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
	 * @return boolean true if the value is successfully stored into cache, false otherwise
	 */
	abstract protected function addValue($key,$value,$expire);
	/**
	 * 按key删除一个缓存
	 * 
	 * 抽象方法，子类使用
	 * 
	 * This method should be implemented by child classes to delete the data from actual cache storage.
	 * @param string the key of the value to be deleted
	 * @return boolean if no error happens during deletion
	 */
	abstract protected function deleteValue($key);
}
/*
+---------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Wed Sep 05 17:29:49 CST 2007
+--------------------------------------------------------------------------
*/
?>